<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts-wordcloud')
require('./spider') // echarts theme
import { debounce } from '@/utils'
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    hots:{
      type: Array,
      default: []
    }
  },
  data() {
    return {
      chart: null,
      option:{
        title: {
          padding: 10,
        },
        tooltip: {
          show: false
        },
        series: [{
          type: 'wordCloud',
          gridSize: 1,
          sizeRange: [12, 55],
          rotationRange: [-45, 0, 45, 90],
          textStyle: {
            fontFamily: 'sans-serif',
            fontWeight: 'bold',
            // Color can be a callback function or a color string
            color: function () {
              // Random color
              return 'rgb(' + [
                Math.round(Math.random() * 160),
                Math.round(Math.random() * 160),
                Math.round(Math.random() * 160)
              ].join(',') + ')';
            }
          },
          left: 'center',
          top: 'center',
          right: null,
          bottom: null,
          data: this.hots
        }]
      }
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  watch:{
    hots(newValue,oldValue){
      console.log("newValue",newValue)
      this.option.series[0].data=newValue
      this.chart.setOption(this.option);
    },

  },
  methods: {
    initChart() {

      this.chart = echarts.init(this.$el, 'spider')
      this.chart.setOption(this.option)

    }
  }
}
</script>
